/*
   Copyright (c) 2011, 2012 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-18 Marco Riccardi - initial implementation
 *  2012-04-06 Marco Riccardi - added message in DliParseException
 *  2012-04-09 Marco Riccardi - added "redreg" reduction registration
 *  and "sethood" property
 *
 */
package org.mizartools.dli;

public class DecodedLibraryItem {
	
	ItemId itemId = null;
	ItemDefinition itemDefinition = null;
	
	public ItemId getItemId() {
		return itemId;
	}

	public ItemDefinition getItemDefinition() {
		return itemDefinition;
	}
	
	public static DecodedLibraryItem newInstance(String string) throws DliParseException{
		DecodedLibraryItem decodedLibraryItem = new DecodedLibraryItem();
		int position = string.indexOf("=");
		if (position == -1) {
			throw new DliParseException("["+string+"] is not valid in DecodedLibraryItem");
		}
		decodedLibraryItem.itemId = ItemId.newInstance(string.substring(0, position));
		switch (decodedLibraryItem.itemId.itemType){
		case aggr : 
			decodedLibraryItem.itemDefinition = Aggregate.newInstance(string.substring(position+1, string.length()));
			break;
		case aggrnot : 
			decodedLibraryItem.itemDefinition = AggregateNotation.newInstance(string.substring(position+1, string.length()));
			break;
		case attrnot : 
			decodedLibraryItem.itemDefinition = AttributeNotation.newInstance(string.substring(position+1, string.length()));
			break;
		case attr : 
			decodedLibraryItem.itemDefinition = Attribute.newInstance(string.substring(position+1, string.length()));
			break;
		case condreg : 
			decodedLibraryItem.itemDefinition = ConditionalRegistration.newInstance(string.substring(position+1, string.length()));
			break;
		case def : 
			decodedLibraryItem.itemDefinition = Theorem.newInstance(string.substring(position+1, string.length()));
			break;
		case dfs : 
			decodedLibraryItem.itemDefinition = Definiens.newInstance(string.substring(position+1, string.length()));
			break;
		case exreg :
			decodedLibraryItem.itemDefinition = ExistentialRegistration.newInstance(string.substring(position+1, string.length()));
			break;
		case idreg :
			decodedLibraryItem.itemDefinition = IdentifyRegistration.newInstance(string.substring(position+1, string.length()));
			break;
		case func :
			decodedLibraryItem.itemDefinition = Functor.newInstance(string.substring(position+1, string.length()));
			break;
		case funcnot :
			decodedLibraryItem.itemDefinition = FunctorNotation.newInstance(string.substring(position+1, string.length()));
			break;
		case funcreg :
			decodedLibraryItem.itemDefinition = TermAdjectiveRegistration.newInstance(string.substring(position+1, string.length()));
			break;
		case mode :
			decodedLibraryItem.itemDefinition = Mode.newInstance(string.substring(position+1, string.length()));
			break;
		case modenot :
			decodedLibraryItem.itemDefinition = ModeNotation.newInstance(string.substring(position+1, string.length()));
			break;
		case pred :
			decodedLibraryItem.itemDefinition = Predicate.newInstance(string.substring(position+1, string.length()));
			break;
		case prednot :
			decodedLibraryItem.itemDefinition = PredicateNotation.newInstance(string.substring(position+1, string.length()));
			break;
		case redreg :
			decodedLibraryItem.itemDefinition = ReductionRegistration.newInstance(string.substring(position+1, string.length()));
			break;
		case sch :
			decodedLibraryItem.itemDefinition = Scheme.newInstance(string.substring(position+1, string.length()));
			break;
		case sel :
			decodedLibraryItem.itemDefinition = Selector.newInstance(string.substring(position+1, string.length()));
			break;
		case selnot :
			decodedLibraryItem.itemDefinition = SelectorNotation.newInstance(string.substring(position+1, string.length()));
			break;
		case sethood :
			decodedLibraryItem.itemDefinition = Sethood.newInstance(string.substring(position+1, string.length()));
			break;
		case struct :
			decodedLibraryItem.itemDefinition = Structure.newInstance(string.substring(position+1, string.length()));
			break;
		case structnot :
			decodedLibraryItem.itemDefinition = StructureNotation.newInstance(string.substring(position+1, string.length()));
			break;
		case th :
			decodedLibraryItem.itemDefinition = Theorem.newInstance(string.substring(position+1, string.length()));
			break;
		case forgnot :
			decodedLibraryItem.itemDefinition = ForgNotation.newInstance(string.substring(position+1, string.length()));
			break;
		default : 
			throw new DliParseException("["+string+"] is not valid in DecodedLibraryItem");
		}
		return decodedLibraryItem;
	}

	public DecodedLibraryItem(ItemId itemId, ItemDefinition itemDefinition) throws DliException {
		if (itemId == null || itemDefinition == null){
			throw new DliException();
		}
		this.itemId = itemId;
		this.itemDefinition = itemDefinition;
	}
	
	protected DecodedLibraryItem() {}

	@Override
	public String toString() {
		return itemId.toString()+"="+itemDefinition.toString();
	}
	
}
